<template>
  <div class="app-container2">
    <!--工具栏-->
    <div class="head-container">
      <content-block>
        <div>
          <!-- 查询条件 -->
          <el-form :model="searchForm" label-width="100px" class="search-form">
            <el-row>
              <el-col :span="8">
                <el-form-item label="单位编号">
                  <unit-rpc ref="unitRpc" @unitRpcCallBack="unitRpcCallBack" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="单位名称">
                  <el-input v-model="searchForm.aab004" placeholder="请输入单位名称" :disabled="true" />
                </el-form-item>
              </el-col>
              <el-col :span="8" class="text-right" style="float: right">
                <el-form-item>
                  <el-button type="primary" @click="search">查询</el-button>
                  <el-button @click="reset">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </content-block>

      <content-block label="单位信息">
        <el-alert
          v-if="true"
          title="温馨提示"
          type="info"
          description="双击查看人员支出明细"
          show-icon
        />
        <!--表单组件-->
        <el-dialog :visible.sync="dialogVisible" :before-close="fnClose" title="人员支出信息" width="1300px">
          <content-block label="查询条件">
            <el-form ref="form" :model="personForm" :rules="rules" size="small" label-width="80px">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="人员编号">
                    <person-rpc ref="personRpc" @personRpcCallBack="personRpcCallBack" />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="姓名">
                    <el-input v-model="personForm.aac003" placeholder="请输入姓名" :disabled="true" />
                  </el-form-item>
                </el-col>
                <el-col :span="6" class="text-right" style="float: right">
                  <el-form-item>
                    <el-button type="primary" @click="searchPerson">查询</el-button>
                    <el-button @click="resetPerson">重置</el-button>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </content-block>
          <content-block label="支出情况">
            <!--表格渲染-->
            <el-table
              ref="table"
              v-loading="crud.loading"
              :show-summary="true"
              :summary-method="getSummaries"
              stripe
              height="calc(100vh - 510px)"
              :data="crud.data"
              size="small"
              style="width: 100%;"
              border
              :cell-style="moneyCellStyle"
              :header-cell-style="{background:'#f5f7fa', color:'#777'}"
            >
              <!-- 列定义保持不变 -->
              <el-table-column prop="xh" label="序号" type="index" width="45" fixed="left" />
              <el-table-column prop="aac001" label="个人编号" width="130px" fixed="left" align="center" header-align="center" />
              <el-table-column prop="aac003" label="姓名" width="120px" fixed="left" align="center" header-align="center" />
              <!-- 为所有金额列添加金额格式化 -->
              <el-table-column prop="je1" label="正常养老保险剩余金额" width="150px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
              <el-table-column prop="je2" label="改革前划转剩余金额" width="150px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
              <el-table-column prop="je1_tf" label="正常养老保险退费金额" width="150px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
              <el-table-column prop="je2_tf" label="改革前划转退费金额" width="150px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
              <el-table-column prop="je1_zy" label="正常养老保险转移支出金额" width="170px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
              <el-table-column prop="je2_zy" label="改革前划转转移支出金额" width="160px" fixed="left" align="center" header-align="center" :formatter="formatMoney" />
            </el-table>
            <pagination />
          </content-block>
          <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="fnClose">取消</el-button>
          </div>
        </el-dialog>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" height="calc(100vh - 510px)" :data="crud.data" size="small" style="width: 100%;" border :header-cell-style="{background:'#f5f7fa', color:'#777'}" @selection-change="crud.selectionChangeHandler" @row-dblclick="handleRowDblClick">
          <el-table-column prop="xh" label="序号" type="index" width="55" fixed="left" />
          <el-table-column prop="aab001" label="单位编号" width="150px" fixed="left" align="center" header-align="center" />
          <el-table-column prop="aab004" label="单位名称" width="200px" fixed="left" align="center" header-align="center" />
          <el-table-column prop="aab004_y" label="原单位名称" width="150px" align="center" header-align="center" />
          <el-table-column prop="aab998" label="统一社会信用代码" width="150px" align="center" header-align="center" />
          <el-table-column prop="aaa149" label="行业风险等级" width="150px" :formatter="formatter" align="center" header-align="center" />
          <el-table-column prop="aab019" label="单位类型" width="150px" :formatter="formatter" align="center" header-align="center" />
          <el-table-column prop="bab009" label="单位联系人" width="150px" align="center" header-align="center" />
          <el-table-column prop="bab012" label="单位联系电话" width="150px" align="center" header-align="center" />
          <el-table-column prop="aab361" label="单位注册地" width="180px" align="center" header-align="center" />
          <el-table-column prop="aab013" label="法人姓名" width="150px" align="center" header-align="center" />
          <el-table-column prop="aae005" label="法人联系电话" width="150px" align="center" header-align="center" />
          <el-table-column prop="aac147" label="法人证件号码" width="150px" align="center" header-align="center" />
          <el-table-column prop="aae013" label="备注" width="200" align="left" header-align="center" />
        </el-table>
        <!--分页组件-->
        <pagination />
      </content-block>
    </div>
  </div>
</template>

<script>
import personPayoutQuery from '@/views/ynsb_comm/personPayoutQuery/personPayoutQuery'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import pagination from '@crud/Pagination'
import UnitRpc from '@/views/ynsb_comm/comm/module/unitRpc'
import ContentBlock from '@/components/labers/contentBlock'
import PersonRpc from '@/views/ynsb_comm/comm/module/personRpc'

const defaultForm = { aab361: null, bab009: null, bab012: null, aab001: null, aab004: null, aab004_y: null, aab998: null, aab022: null, aaa149: null, aab019: null, aab013: null, aae005: null, aac147: null, aae013: null }
export default {
  name: 'PersonPayoutQuery',
  components: { PersonRpc, ContentBlock, pagination, UnitRpc },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '单位人员支出信息查询', url: 'api/basicAb01', idField: 'aab001', sort: 'aab001,desc', crudMethod: { ...personPayoutQuery }})
  },
  // 数据字典
  dicts: ['aaa149', 'aab019'],
  data() {
    return {
      searchForm: {
        aab001: '', // 单位编号
        aab004: '' // 单位名称
      },
      personForm: {
        aac001: '', // 人员编号
        aac003: '' // 姓名
      },
      rules: {
      },
      dialogVisible: false, // 控制弹窗的显示与隐藏
      selectedRow: null, // 存储被双击的行数据
      // 需要合计的列配置
      summaryColumns: [
        { prop: 'je1', label: '养老保险总额' },
        { prop: 'je2', label: '划转总额' }
      ],
      aab001: '' // 单位编号
    }
  },
  methods: {
    moneyCellStyle({ row, column, rowIndex, columnIndex }) {
      // 为所有金额列设置绿色（列索引 3-8 是金额列）
      if (columnIndex >= 3 && columnIndex <= 8) {
        console.log(row, 'row')
        console.log(column, 'column')
        if ((column.property === 'je1_tf' && row.je1_tf < 0) || (column.property === 'je2_tf' && row.je2_tf < 0) ||
          (column.property === 'je1_zy' && row.je1_zy < 0) || (column.property === 'je2_zy' && row.je2_zy < 0)) {
          return {
            color: 'red', // 红色
            fontWeight: 'bold'
          }
        } else {
          return {
            color: '#07c160', // 微信绿色
            fontWeight: 'bold'
          }
        }
      }
      return {}
    },
    // 合计行计算方法
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      // 需要求和的列（prop名称）
      const sumColumns = [
        'je1', 'je2', 'je1_tf', 'je2_tf', 'je1_zy', 'je2_zy'
      ]

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        // 检查当前列是否需要求和
        if (sumColumns.includes(column.property)) {
          const values = data.map(item => Number(item[column.property]))
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            return isNaN(value) ? prev : prev + value
          }, 0)
          console.log(this.formatMoney(sums[index]), 'sums[index]')
          sums[index] = (sums[index] != null) ? `￥${(Number(sums[index]).toFixed(2) + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')}` : '--'
        } else {
          sums[index] = '--'
        }
      })
      return sums
    },
    formatter(row, column, cellValue) {
      // 根据 aae140 的值进行转码
      const key = column.property
      const data = this.dict[key]
      const dataList = data.filter(item => item.value === cellValue)
      console.log(dataList, 'data')
      if (dataList !== null && dataList !== [] && dataList !== undefined && dataList.length > 0) {
        return dataList[0].label
      } else {
        return cellValue
      }
    },
    formatMoney(row, column, cellValue) {
      const value = (cellValue != null) ? `￥${(Number(cellValue).toFixed(2) + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,')}` : '--'
      // return {
      //   value,
      //   class: isPositive ? 'money-cell positive' : 'money-cell'
      // }
      return value
    },
    personRpcCallBack(item) {
      this.personForm.aac001 = item.aac001
      this.personForm = {
        aac001: item.aac001,
        aac003: item.aac003
      }
    },
    search() {
      // 假设查询的数据
      this.crud.params = {
        aab001: this.searchForm.aab001,
        queryType: '1',
        aab004: this.searchForm.aab004
      }
      this.crud.url = 'api/basicAb01'
      this.crud.pageChangeHandler(1)
    },
    searchPerson() {
      this.crud.params = {
        aac001: this.personForm.aac001,
        queryType: '2',
        aab001: this.aab001
      }
      this.crud.url = 'api/personPayoutQuery/list'
      this.crud.pageChangeHandler(1)
    },
    reset() {
      this.$refs.unitRpc.restAab001('')
      this.searchForm = {
        aab001: '',
        aab004: ''
      }
      this.tableData = [] // 清空表格数据
    },
    resetPerson() {
      this.$refs.personRpc.restAac001('')
      this.personForm = {
        aac001: '',
        aac003: ''
      }
      this.tableData = [] // 清空表格数据
    },
    unitRpcCallBack(item) {
      this.form.aab001 = item.aab001
      this.searchForm = {
        aab001: item.aab001,
        aab004: item.aab004
      }
    },
    // 双击行触发的事件
    handleRowDblClick(row) {
      this.crud.data = []
      this.aab001 = row.aab001
      this.selectedRow = row // 获取双击的行数据
      this.dialogVisible = true // 显示弹窗
      setTimeout(() => {
        this.crud.params = {
          aac001: this.personForm.aac001,
          queryType: '2',
          aab001: this.aab001
        }
        this.crud.url = 'api/personPayoutQuery/list'
        this.crud.pageChangeHandler(1)
      }, 500) // 延迟1秒后显示弹窗
    },
    // 关闭事件
    fnClose() {
      this.dialogVisible = false
      // 关闭弹窗
      setTimeout(() => {
        this.crud.params = {
          aab001: this.searchForm.aab001,
          queryType: '1',
          aab004: this.searchForm.aab004
        }
        this.crud.url = 'api/basicAb01'
        this.crud.pageChangeHandler(1)
      }, 500) // 延迟1秒后显示弹窗
    },
    queryCallBack() {
      this.dialogVisible = false // 显示弹窗
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
/* 自定义金额单元格样式 */
.el-table ::v-deep .money-cell {
  font-weight: bold;
  color: #f56c6c; /* 红色强调 */
}

.el-table ::v-deep .money-cell.positive {
  color: #67c23a; /* 绿色正数 */
}
</style>
